﻿using System;
using System.Data;
using System.Text;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;

namespace Ledict.ConditionExtract.DAL
{
    public class WordBankSynonymDAL
    {
        private readonly string DbName;

        public WordBankSynonymDAL()
        {
            DbName = "JudgeDoc";
        }

        /// <summary>
        ///     是否存在该记录
        /// </summary>
        public bool Exists(string WBS_SynonymWords)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from SYS_WordBank_Synonym");
            strSql.Append(" where WBS_SynonymWords=@WBS_SynonymWords ");
            var oBase = DatabaseFactory.CreateDatabase(DbName);
            var oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WBS_SynonymWords", DbType.String, WBS_SynonymWords);

            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        ///     增加一条数据
        /// </summary>
        public bool Add(SYS_WordBank_Synonym Model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into SYS_WordBank_Synonym(");
            strSql.Append("WBS_ID,WBS_SynonymWords,WBS_Time)");
            strSql.Append(" values (");
            strSql.Append("@WBS_ID,@WBS_SynonymWords,@WBS_Time)");
            var oBase = DatabaseFactory.CreateDatabase(DbName);
            var oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WBS_ID", DbType.String, Model.WBS_ID);
            oBase.AddInParameter(oCmd, "@WBS_SynonymWords", DbType.String, Model.WBS_SynonymWords);
            oBase.AddInParameter(oCmd, "@WBS_Time", DbType.DateTime, DateTime.Now);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update(SYS_WordBank_Synonym Model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update SYS_WordBank_Synonym set ");
            strSql.Append("WBS_SynonymWords=@WBS_SynonymWords,");
            strSql.Append("WBS_Time=@WBS_Time");
            strSql.Append(" where WBS_ID=@WBS_ID ");
            var oBase = DatabaseFactory.CreateDatabase(DbName);
            var oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WBS_ID", DbType.String, Model.WBS_ID);
            oBase.AddInParameter(oCmd, "@WBS_SynonymWords", DbType.String, Model.WBS_SynonymWords);
            oBase.AddInParameter(oCmd, "@WBS_Time", DbType.DateTime, DateTime.Now);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(string WBS_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from SYS_WordBank_Synonym ");
            strSql.Append(" where WBS_ID=@WBS_ID ");
            var oBase = DatabaseFactory.CreateDatabase(DbName);
            var oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WBS_ID", DbType.String, WBS_ID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public DataTable GetList(string WhereText)
        {
            var strSql = new StringBuilder();
            strSql.Append(
                "select row_number()over(order by WBS_Time desc) as showindex,WBS_ID,WBS_SynonymWords,WBS_Time ");
            strSql.Append(" FROM SYS_WordBank_Synonym  ");
            if (WhereText != "")
                strSql.Append(" where  WBS_SynonymWords like  '%'+@WhereText+'%'");

            var oBase = DatabaseFactory.CreateDatabase(DbName);
            var oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@WhereText", DbType.String, WhereText);

            var OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            return new DataTable();
        }
    }
}